<!--
- File:    花园详细页.
-->
<template>
  <div class="Web_Box">
    <div class="task_top f-cb" style="position: inherit">
      <p class="task_b" style="z-index: 99;position: fixed; width: 100%">未完成的任务({{taskCount}})<a v-show="!taskVisible" class="fr" @click="switchTaskShow(true)">查看详情</a><a v-show="taskVisible" class="fr" @click="switchTaskShow(false)">关闭</a>
      </p>
      <div class="PopBg" @click="switchTaskShow(false)" v-show="taskVisible"></div>
      <!--任务弹窗-->
      <div class="task_cont" v-show="taskVisible" style="z-index: 90">
        <div class="index_menu_box2">
          <div class="index_menu2">
            <ul class="f-cb" style="width:30rem;">
              <li @click="switchTaskType('0')"><a :class="{'cur': currentTaskType == '0'}">花园任务</a></li>
              <li v-for="plant in taskPlantList" @click="selectCurrentPlantTask(plant)">
                <a :class="{'cur': currentTaskType == '1' && currentPlantTask.o_id == plant.o_id}">{{plant.m_name}}</a>
              </li>
            </ul>
          </div>
        </div>
        <div class="task_list">
          <div class="watering_list" v-show="currentTaskType == '0'">
            <div class="watering"><span>{{gardenTaskInfo.ta_title || '花园任务未设置'}}</span><span style="font-size: 12px; color: #aaa"> (编号:{{parentId ? parentId + '-': ''}}{{gardenId}}-{{gardenTaskInfo.gt_id}})</span></div>
            <div class="water_cont">
              <ul>
                <li>时隔天数   {{gardenTaskInfo.ta_interval}}{{gardenTaskInfo.ta_interval_type | taskIntervalTxt}}</li>
                <li>提醒时间   {{gardenTaskInfo.ta_time}}</li>
                <li v-html="gardenTaskInfo.ta_note"></li>
              </ul>
            </div>
          </div>
          <div v-show="currentTaskType == '1'">
            <div class="watering_list" v-for="(task, taskIndex) in currentPlantTask.task">
              <div class="watering">
                <i class="iconfont icon-red2"></i><span>{{task.ta_type_name}}</span>
                <span style="font-size: 12px; color: #aaa"> (编号:{{parentId ? parentId + '-': ''}}{{gardenId}}-{{task.o_id}}-{{task.mt_id}})</span>
                <a class="water_btn" @click="gotoAddGrowthNote(task)">去完成</a><span class="hl" @click="cancleTask">忽略</span>
              </div>
              <div class="water_cont">
                <ul>
                  <li>时隔天数   {{task.ta_interval}}{{task.ta_interval_type | taskIntervalTxt}}</li>
                  <li>提醒时间   {{task.ta_time}}</li>
                  <li v-html="task.ta_note"></li>
                </ul>
              </div>
            </div>
          </div>
          <div class="waterimg_btn"><a @click="gotoAllTask">全部任务</a><a @click="switchTaskShow(false)">收起</a></div>
        </div>
      </div>
      <!--任务弹窗 end-->
    </div>
    <div style="margin-top: 38px">
    </div>
    <!--天气-->
    <div class="weather_box f-cb" v-if="weather">
      <img :src="$config.imgsite + $config.uploadImgSrc + weather.icon">
      <div class="fl weath_tit">
        <h3>{{areaName}}</h3>
        <p>{{weather.text}}  {{weather.temp}}°C</p>
      </div>
      <!--<i class=" iconfont icon-more"></i>-->
    </div>
    <!--天气-->
    <div class="add_gran" v-show="isCreator == '1'"><a class="add_btn" @click="gotoSelectPlant">添加新植物</a></div>
    <div class="Release2 f-cb pl20 tc" v-show="isCreator == '1'">
      <a class="rbtn" @click="showGardenSheet" v-show="isDefault != 'true'" style="margin-right: 2rem"><i class="iconfont icon-bianji"></i>编辑花园</a><a class="rbtn2" @click="gotoUpdateGardenTask"><i class="iconfont icon-bianji"></i>编辑任务</a>
    </div>
    <!--花园-->
    <div style="position: relative" v-infinite-scroll="loadMore"
      infinite-scroll-disabled="false"
      infinite-scroll-distance="0"
      infinite-scroll-immediate-check="false">
      <div class="graden_cont" v-for="plant in plantList.data">
        <h3 class="j_name" v-show="gardenType == $dictionary.gardenType.share">
          <span>管理员：{{plant.creator_name}}</span>
          <span style="margin-left: 15px;" v-show="plant.is_assign == '1'">园丁：{{plant.member_name}}</span>
          <span style="margin-left: 15px;" v-show="plant.is_assign == '0'">未分配园丁</span>
        </h3>
        <ul class="f-cb">
          <li class="mood_graden">
            <a @click="gotoPlantDetail(plant)"><img :src="(plant.flower_image || plant.m_goods_image) ? $config.imgsite + $config.uploadImgSrc + (plant.flower_image || plant.m_goods_image) + '_w200' : defaultPlantImg">
            </a>
            <span class="num-notice" @click.stop="showPlantTask(plant)" v-show="plant.task_num > 0">{{plant.task_num}}</span>
          </li>
          <li>
            <div class="graden_bg" style="min-height: 80px">
              <div style="position: absolute; right: 0; top: 0; width: 26px; height: 26px" v-show="plant.d_image">
                <img :src="$config.imgsite + $config.uploadImgSrc + plant.d_image + '_w150'">
              </div>
              <i v-show="plant.growth_status == 0" class="iconfont icon-tab_potato_heighlight"></i>
              <i v-show="plant.growth_status == 1" class="iconfont icon-zhongzi"></i>
              <i v-show="plant.growth_status == 2" class="iconfont icon-shuye1"></i>
              <i v-show="plant.growth_status == 3" class="iconfont icon-shu1"></i>
              <h3 @click="gotoPlantDetail(plant)">
                <a>{{plant.flower_name || plant.m_name}} <span style="color: #aaa; font-size: 12px;">(编号:{{plant.sg_id != 0 ? plant.sg_id + '-' : ''}}{{plant.g_id}}-{{plant.o_id}})</span></a>
              </h3>
              <p style="font-size: 12px" v-show="gardenType == 1">添加时间：{{plant.add_time | timestampToDate('yyyy-MM-dd')}}</p>
              <p style="font-size: 12px" v-show="gardenType == 2 && (plant.start_time || plant.end_time)">共享周期：{{plant.start_time | timestampToDate('yyyy-MM-dd')}}~{{plant.end_time | timestampToDate('yyyy-MM-dd')}}</p>
              <p style="font-size: 12px" v-show="plant.last_tutorial">最近养护：{{plant.last_tutorial | timestampToDate('yyyy-MM-dd hh:mm')}}</p>
              <div class="Release3 f-cb" v-show="isCreator == '1'">
                <a class="rbtn"><span @click="showPlantSheet(plant.o_id)"><i class="iconfont icon-bianji"></i>编辑植物</span></a>
              </div>
            </div>
          </li>
        </ul>
        <div class="Jurisdiction" v-show="gardenType == $dictionary.gardenType.share && isCreator == '1'">
          <ul class="f-cb">
            <li class="bor-r-eee" @click="gotoModify(plant.o_id, plant.member_id, plant.sg_id)">
              <a><i class="iconfont icon-bi"></i>修改权限</a></li>
            <li @click="delPlantDistribute(plant)">
              <span :class="{'cr_aaa': plant.is_assign == '0'}"><i class="iconfont icon-jiechuicon01"></i>解除权限</span>
            </li>
          </ul>
        </div>
      </div>
    </div>
    <empty-data :show="plantList.allLoaded && plantList.data.length <= 0" style="margin-top: 3rem"></empty-data>
    <loading-more :allLoaded="plantList.allLoaded" :show="plantList.data.length > 0"></loading-more>
    <footer-tab></footer-tab>
    <mt-actionsheet
      :actions="gardenActions"
      v-model="gardenSheetVisible">
    </mt-actionsheet>
    <mt-actionsheet
      :actions="plantActions"
      v-model="plantSheetVisible">
    </mt-actionsheet>
  </div>
</template>

<script>
  import {Actionsheet} from 'mint-ui'

  export default{
    data() {
      return {
        gardenType: this.$route.query.gardenType || this.$dictionary.gardenType.private, // 是否是共享花园
        gardenId: this.$route.params.id,
        parentId: this.$route.query.parentId,
        isCreator: this.$route.query.isCreator,
        isDefault: this.$route.query.isDefault || 'true', // 是否是默认花园（心情花园）
        areaId: this.$route.query.areaId || this.$storage.getLocation().code,
        areaName: this.$route.query.areaName || this.$storage.getLocation().name,
        defaultPlantImg: require('../../assets/images/demo/xinqing2.png'),
        weather: null,
        plantList: {requested: false, loading: false, allLoaded: false, page: 1, pageSize: 10, data: []},
        taskVisible: false,
        gardenSheetVisible: false,
        gardenActions: [{
          name: '修改信息',
          method: this.updateGarden
        }, {
          name: '删除花园',
          method: this.delGarden
        }],
        plantSheetVisible: false,
        currentEditPlantId: '',
        plantActions: [{
          name: '修改信息',
          method: this.gotoEditPlant
        }, {
          name: '删除植物',
          method: this.delPlant
        }],
        taskPlantList: [], // 花园里的植物任务列表
        gardenTaskInfo: {}, // 花园任务
        currentTaskType: '0', // 当前显示的任务类型：'0' 花园任务， '1' 植物任务
        currentPlantTask: {} // 当前显示的植物任务
      }
    },
    components: {
      'mt-actionsheet': Actionsheet
    },
    computed: {
      taskCount: function () {
        let count = 0
        for (let i = 0; i < this.taskPlantList.length; i++) {
          for (let j = 0; j < this.taskPlantList[i].task.length; j++) {
            count++
          }
        }
        return count
      }
    },
    created() {
      console.log('tt', this.$route.taskVisible, this.$utils.getQueryString('taskVisible'))
      this.getPlantList(this.plantList.page, this.plantList.pageSize)
      this.getTaskListByGarden()
      this.getGardenTaskInfo()
      this.getWeatherInfo()
    },
    methods: {
      showPlantTask(plant) {
        console.log('任务', plant)
        this.currentTaskType = '1'
        for (let i = 0; i < this.taskPlantList.length; i++) {
          if (this.taskPlantList[i].o_id == plant.o_id) {
            this.currentPlantTask = this.taskPlantList[i]
            break
          }
        }
        this.switchTaskShow(true)
      },
      gotoAllTask() {
        this.$router.push({name: 'task-list'})
      },
      showGardenSheet() {
        this.gardenSheetVisible = true
      },
      showPlantSheet(id) {
        this.plantSheetVisible = true
        this.currentEditPlantId = id
      },
      switchTaskShow(boolValue) {
//        if (boolValue) {
//          this.$utils.addBodyFix() // 显示模态框，防止滚动模态框里的div时body也滚动
//        } else {
//          this.$utils.removeBodyFix()
//        }
        this.taskVisible = boolValue
      },
      gotoPlantDetail(plant) {
        this.$router.push({
          name: 'plant-detail',
          params: {id: plant.o_id},
          query: {isCurator: plant.is_curing, isCreator: this.isCreator, title: plant.flower_name}
        })
      },
      gotoSelectPlant() {
        this.$router.push({
          name: 'wiki-list',
          query: {action: 'select', garden: this.gardenId, gardenType: this.gardenType, parentId: this.parentId}
        })
      },
      gotoEditPlant() {
        this.$router.push({name: 'plant-edit', params: {id: this.currentEditPlantId}})
      },
      gotoUpdateGardenTask() {
        this.$router.push({name: 'task-garden', params: {id: this.gardenId}})
      },
      gotoAddGrowthNote(task) {
        this.$router.push({
          name: 'growth-add',
          query: {plant: task.o_id, action: task.ta_type, status: task.growth_status}
        })
      },
      gotoModify(plantId, memberId, gardenId) {
        this.$router.push({name: 'distribute-modify', query: {plant: plantId, member: memberId, garden: gardenId}})
      },
      getTaskListByGarden() {
        this.$request.getTaskListByGarden(this.gardenId).then(data => {
          this.taskPlantList = data
        })
      },
      getWeatherInfo() {
        this.$request.getWeatherInfo(this.areaId).then(data => {
          if (data && data.length > 0) {
            this.weather = data
          }
        })
      },
      delPlant() {
        this.$messageBox.confirm('确定删除植物？').then(data => {
          this.$request.delPlantInGarden(this.currentEditPlantId).then(data => {
            this.$toast('删除植物成功')
            this.plantList = {requested: false, loading: false, allLoaded: false, page: 1, pageSize: 10, data: []}
            this.getPlantList(this.plantList.page, this.plantList.pageSize)
          }).catch(e => {
            this.$toast('删除失败:' + e.msg)
          })
        }).catch(() => {
        })
      },
      getPlantList(page, pageSize) {
        this.plantList.loading = true
        this.$request.getPlantListByGarden(this.gardenId, page, pageSize).then(data => {
          if (data.length < this.plantList.pageSize) {
            this.plantList.allLoaded = true
          }
          this.plantList.data = [...this.plantList.data, ...data]
        }).finally(() => {
          this.plantList.requested = true
          this.plantList.loading = false
        })
      },
      loadMore() {
        if (this.plantList.loading || this.plantList.allLoaded) { // 正在加载 或 已取得全部数据
          return
        }
        setTimeout(() => {
          this.getPlantList(++this.plantList.page, this.plantList.pageSize)
        }, 500)
      },
      updateGarden() {
        this.$router.push({name: 'garden-edit', params: {id: this.gardenId}})
      },
      delGarden() {
        this.$messageBox.confirm('确定删除花园？').then(data => {
          this.$request.delGarden(this.gardenId).then(data => {
            this.$toast('删除成功')
            this.$router.go(-1)
          })
        }).catch(() => {
        })
      },
      getGardenTaskInfo() {
        this.$request.getGardenTaskInfo(this.gardenId).then(data => {
          this.gardenTaskInfo = data
        })
      },
      switchTaskType(value) {
        this.currentTaskType = value
      },
      selectCurrentPlantTask(plant) {
        this.currentTaskType = '1'
        this.currentPlantTask = plant
      },
      cancleTask(id, index) {
        this.$request.cancleTask(id).then(data => {
          this.currentPlantTask.task.splice(index, 1)
          if (this.currentPlantTask.task.length <= 0) { // 该植物所有任务都已处理，移除该植物
            for (let i = 0; i < this.taskPlantList.length; i++) {
              if (this.currentPlantTask.o_id == this.taskPlantList[i].o_id) {
                this.taskPlantList.splice(i, 1)
                this.currentPlantTask = this.taskPlantList[0]
                break
              }
            }
          }
          this.$toast('忽略成功')
        })
      },
      delPlantDistribute(plant) {
        if (plant.is_assign == '0') {
          return
        }
        this.$messageBox.confirm(`你确定要解除吗?`).then(action => {
          this.$request.delPlantDistribute(plant.o_id).then(data => {
            plant.is_assign = '0'
            plant.assign_id = plant.creator_id
            plant.member_id = plant.creator_id
          })
        }).catch(() => {
        })
      }
    }
  }
</script>

<style>
</style>